package ca.concordia.soen6461.wichf.navigation.view;

import java.util.Calendar;

import ca.concordia.soen6461.wichf.controller.Event;
import ca.concordia.soen6461.wichf.controller.View;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent;
import ca.concordia.soen6461.wichf.entity.User;
import ca.concordia.soen6461.wichf.service.SchedulingService;

public class HomeView implements View<HomeViewBean> {

	public static final int MINUTE = 60 * 1000;
	public static final int MIN_TIME_SLOT_IN_MINUTES = 20; 
	public static final int MIN_TIME_SLOT = MIN_TIME_SLOT_IN_MINUTES * MINUTE; 
	public static final int SLOTS_FOR_VISIT = 1; 
	public static final int SLOTS_FOR_CHECKUP = 3;

	private View<HomeViewBean> patientView;
	private View<HomeViewBean> doctorView;
	
	public HomeView(SchedulingService schedulingService) {
		this.patientView = new PatientHomeView(schedulingService);
		this.doctorView = new DoctorHomeView(schedulingService);
	}

	@Override
	public HomeViewBean render(Event event) {
		View<HomeViewBean> view =
			event.getSession().get(User.class).getRoles().contains(User.Role.DOCTOR)
			? doctorView
			: patientView;
		return view.render(event);
	}

	public static Calendar getDate(int d, int t) {
	    Calendar date = getDate();
		date.add(Calendar.DATE, d + 1);
		date.add(Calendar.MINUTE, t * MIN_TIME_SLOT_IN_MINUTES);
	    return date;
	}

	public static Calendar getDate() {
	    Calendar date = Calendar.getInstance();
	    resetTime(date);
	    return date;
	}

	public static void resetTime(Calendar time) {
		time.set(Calendar.HOUR_OF_DAY, 8);
        time.set(Calendar.MINUTE, 0);
        time.set(Calendar.SECOND, 0);
        time.set(Calendar.MILLISECOND, 0);
	}

	public static int duration(ScheduledEvent.Type type) { 
		return (
			type == ScheduledEvent.Type.PatientCheckup 
			? SLOTS_FOR_CHECKUP 
			: SLOTS_FOR_VISIT
		) * MIN_TIME_SLOT_IN_MINUTES;
	}
	
}
